package com.lcm.algorithm.datatest.sort;

import java.util.Arrays;

/**
 * @description:
 * @author: lcm
 * @create: 2020-04-08 16:01
 **/

public class BubbleSort {

    public static void main(String[] args) {
        int[] n = new int[]{6, 5, 2, 7, 3, 9, 8, 4, 10, 1};
        sort(n);
        System.out.println(Arrays.toString(n));
    }

    public static void sort(int[] array) {
        //数组是否已经有序标记
        boolean isSort;
        //数组无序边界
        int sortIndex = array.length - 1;
        //最后交换位置
        int lastSwapIndex = array.length - 1;
        for (int i = 0; i < array.length; i++) {
            isSort = true;
            for (int j = 0; j < sortIndex; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    isSort = false;
                    lastSwapIndex = j;
                }
            }
            if (isSort) {
                break;
            }
            sortIndex = lastSwapIndex;
        }
    }
}
